var api_token = sessionStorage.getItem('api_token');
var tr_id = sessionStorage.getItem('tr_id');
var start_date = sessionStorage.getItem('start_date'); //开始时间
var end_date = sessionStorage.getItem('end_date'); //结束时间
// var http = 'http://c.yueshengkj.com/api/auth/mac?tr_id=' + tr_id;
var http;
if(start_date && end_date){
    http = 'http://c.yueshengkj.com/api/auth/mac?tr_id=' + tr_id + '&start_date=' + start_date + '&end_date=' + end_date; //请求网址
} else {
    http = 'http://c.yueshengkj.com/api/auth/mac?tr_id=' + tr_id
}
function mytime() {
    var t = null;
    t = setTimeout(time, 1000);
    function time() {
        clearTimeout(t);
        var dt = new Date();
        var y = dt.getFullYear();
        var mt = dt.getMonth() + 1;
        var day = dt.getDate();
        var h = dt.getHours();
        var m = dt.getMinutes();
        var s = dt.getSeconds();
        document.getElementById("showTime").innerHTML = y + "年" + mt + "月" + day + "日" + h + "时" + m + "分" + s + "秒";
        t = setTimeout(time, 1000);
    }
}
function getAllData(arg) {
    var ajaxbg = $("#background,#progressBar");
    $.ajax({
        type: "GET",
        headers: {
            "Authorization":'Bearer ' + api_token//此处放置请求到的用户token
        },
        async: true,
        url: http,
        cache: false,
        crossDomain: true == !(document.all),
        data: {
            "@action": arg.join(',')
        },
        dataType: "json",
        beforeSend:function()
        {
            ajaxbg.show();
        },
        success: function (res) {
            for (var i=0; i<arg.length; i++) {
                if (arg[i] == 'information') {
                    information(res.data.information)
                }
            }
            ajaxbg.hide();
        },
        error:function(){
            ajaxbg.hide();
            alert("数据加载失败!");
        }
    });
}
function information(data) {
    for (var i = 0; i < Object.keys(data).length; i++) {
        if (Object.keys(data)[i] == '用户兴趣') {
            businessInterests(data.用户兴趣);
        }
    }
}
function businessInterests(data) {
    var color = [
        ['rgba(0,208,207,0)', 'rgba(0,208,207,1)'],
        ['rgba(255,144,78,0)', 'rgba(255,144,78,1)'],
        ['rgba(47,125,255,0)', 'rgba(47,125,255,1)'],
        ['rgba(139,59,255,0)', 'rgba(139,59,255,1)'],
        ['rgba(223,9,237,0)', 'rgba(223,9,237,1)'],
        ['rgba(107,237,9,0)', 'rgba(107,237,9,1)'],
        ['rgba(241,81,101,0)', 'rgba(241,81,101,1)'],
        ['rgba(113,255,213,0)', 'rgba(113,255,213,1)'],
        ['rgba(33,11,126,0)', 'rgba(33,11,126,1)'],
        ['rgba(143,134,184,0)', 'rgba(143,134,184,1)'],
        ['rgba(248,5,243,0)', 'rgba(248,5,243,1)'],
        ['rgba(174,27,103,0)', 'rgba(174,27,103,1)'],
        ['rgba(164,201,202,0)', 'rgba(164,201,202,1)'],
        ['rgba(168,164,202,0)', 'rgba(168,164,202,1)'],
        ['rgba(246,193,15,0)', 'rgba(246,193,15,1)'],
        ['rgba(149,246,15,0)', 'rgba(149,246,15,1)'],
        ['rgba(80,248,106,0)', 'rgba(80,248,106,1)'],
        ['rgba(80,242,248,0)', 'rgba(80,242,248,1)'],
        ['rgba(248,216,8,0)', 'rgba(248,216,8,1)'],
        ['rgba(142,235,187,0)', 'rgba(142,235,187,1)'],
        ['rgba(237,23,9,0)', 'rgba(237,23,9,1)'],
        ['rgba(180,167,810)', 'rgba(180,167,81,1)'],
        ['rgba(248,243,210,0)', 'rgba(248,243,210,1)'],
        ['rgba(223,248,210,0)', 'rgba(223,248,210,1)'],
        ['rgba(215,168,112,0)', 'rgba(215,168,112,1)'],
        ['rgba(241,133,8,0)', 'rgba(241,133,8,1)'],
        ['rgba(237,23,9,0)', 'rgba(237,23,9,1)'],
        ['rgba(157,130,99,0)', 'rgba(157,130,99,1)']

    ];
    var yAxisData = [], seriesData = [];
    var compare = function (obj1, obj2) {
        var val1 = Number(obj1.占比.replace('%',''));
        var val2 = Number(obj2.占比.replace('%',''));
        if (val1 < val2) {
            return -1;
        } else if (val1 > val2) {
            return 1;
        } else {
            return 0;
        }
    };
    var arrData = data.sort(compare); //数组对象从小到大排序，使用reverse()方法颠倒排序
    for (var i = 0; i < arrData.length; i++) {
        yAxisData.push(data[i].特征);
        seriesData.push(data[i].占比.replace('%',''));
    }
    yAxisData = yAxisData.slice(0,14)
    seriesData = seriesData.slice(0,14)
    var myChart = echarts.init(document.getElementById('tendency'));
    var option = {
        tooltip: {
            show: true,
            formatter: "{b} <br> {c}%"

        },
        grid: {
            left: 130,
            top: 20,
            bottom: 30,
            x: 50
        },
        xAxis: [{
            type: 'value',
            axisTick: {
                show: false,
                // color:'#fff',
            },
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#00DAEE'
                }
            },
            axisLabel: {
                show: true,
                textStyle: {
                    color: '#D0E3FF',
                    fontSize: 10
                }
            },
            splitLine: {
                show: false,
                color: '#D0E3FF'
            }
        }],
        yAxis: [{
            type: 'category',
            // data: ['美食订单订单', '购物', '门票', '交通', '娱乐', '门票', '交通', '娱乐'],
            data: yAxisData,
            // minInterval: 1,
            axisTick: {
                // color:'#fff',
                show: false,
            },
            axisLine: {
                //  color:'#fff',
                show: true,
                lineStyle: {
                    color: '#00DAEE'
                }
            },
            axisLabel: {

                textStyle: {
                    color: '#D0E3FF',
                    fontSize: 12
                }
            }

        }],
        series: [{
            name: '景点排行',
            type: 'bar',
            barWidth: 20,
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: '{c}%',
                    textStyle: {
                        color: '#fff',
                        fontSize: '12'
                    }
                }
            },
            data: []
        }]
    };
    // 使用刚指定的配置项和数据显示图表。
    if (seriesData && 18 >= seriesData.length > 0) {
        for (var j1 = 0; j1 < seriesData.length; j1++) {
            option.series[0].data.push({
                value: seriesData[j1],
                name: yAxisData[j1],
                itemStyle: {
                    normal: {
                        barBorderRadius: [0, 10, 10, 0],
                        color: {
                            type: 'bar',
                            colorStops: [{
                                offset: 0,
                                color: color[j1][0] // 0% 处的颜色
                            }, {
                                offset: 1,
                                color: color[j1][1] // 100% 处的颜色
                            }],
                            globalCoord: false // 缺省为 false

                        }
                    }
                }
            });
        }
    } else if (seriesData && 18 < seriesData.length)  {
        for (var j2 = 0; j2 < 15; j2++) {
            option.series[0].data.push({
                value: seriesData[j2],
                name: yAxisData[j2],
                itemStyle: {
                    normal: {
                        barBorderRadius: [0, 10, 10, 0],
                        color: {
                            type: 'bar',
                            colorStops: [{
                                offset: 0,
                                color: color[j2][0] // 0% 处的颜色
                            }, {
                                offset: 1,
                                color: color[j2][1] // 100% 处的颜色
                            }],
                            globalCoord: false // 缺省为 false

                        }
                    }
                }
            });
        }
    }

    myChart.setOption(option);
}
$(function () {
    mytime();
    echarts2();
    echarts3();
    echarts4();
    getAllData(['information']);
    function echarts2() {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('low'));
        // var shadowColor = '#374b86';
        var option = {
            title: {
                text: 47 + '%',
                left: 'center',
                top: 'center', //top待调整
                textStyle: {
                    color: '#019BFF',
                    fontSize: 26,
                    fontFamily: 'sans-serif'
                }
            },
            series: [{
                name: 'pie1',
                type: 'pie',
                clockWise: true,
                radius: ['70%', '80%'],
                itemStyle: {
                    normal: {
                        label: {
                            show: false
                        },
                        labelLine: {
                            show: false
                        }
                    }
                },
                hoverAnimation: false,
                data: [{
                    value: 47,
                    name: 'completed',
                    itemStyle: {
                        normal: {
                            borderWidth: 5,
                            borderColor: {
                                colorStops: [{
                                    offset: 0,
                                    color: '#009FFF' // 0% 处的颜色
                                }, {
                                    offset: 1,
                                    color: '#4500FF' // 100% 处的颜色
                                }]
                            },
                            color: { // 完成的圆环的颜色
                                colorStops: [{
                                    offset: 0,
                                    color: '#009FFF' // 0% 处的颜色
                                }, {
                                    offset: 1,
                                    color: '#4500FF' // 100% 处的颜色
                                }]
                            },
                            label: {
                                show: false
                            },
                            labelLine: {
                                show: false
                            }
                        }
                    }
                }, {
                    name: 'gap',
                    value: 100 - 47,
                    itemStyle: {
                        normal: {
                            label: {
                                show: false
                            },
                            labelLine: {
                                show: false
                            },
                            color: 'rgb(60,60,60)',
                            borderColor: 'rgb(60,60,60)',
                            borderWidth: 5
                        }
                    }
                }]
            }]
        }
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }
    function echarts3() {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('centre'));
        // var shadowColor = '#374b86';
        var option = {
            title: {
                text: 38 +'%',
                left: 'center',
                top: 'center', //top待调整
                textStyle: {
                    color: '#019BFF',
                    fontSize: 26,
                    fontFamily: 'sans-serif'
                }
            },
            series: [{
                name: 'pie1',
                type: 'pie',
                clockWise: true,
                radius: ['70%', '80%'],
                itemStyle: {
                    normal: {
                        label: {
                            show: false
                        },
                        labelLine: {
                            show: false
                        }
                    }
                },
                hoverAnimation: false,
                data: [{
                    value: 38,
                    name: 'completed',
                    itemStyle: {
                        normal: {
                            borderWidth: 5,
                            borderColor: {
                                colorStops: [{
                                    offset: 0,
                                    color: '#F07D17' // 0% 处的颜色
                                }, {
                                    offset: 1,
                                    color: '#FFAE12' // 100% 处的颜色
                                }]
                            },
                            color: { // 完成的圆环的颜色
                                colorStops: [{
                                    offset: 0,
                                    color: '#F07D17' // 0% 处的颜色
                                }, {
                                    offset: 1,
                                    color: '#FFAE12' // 100% 处的颜色
                                }]
                            },
                            label: {
                                show: false
                            },
                            labelLine: {
                                show: false
                            }
                        }
                    }
                }, {
                    name: 'gap',
                    value: 100 - 38,
                    itemStyle: {
                        normal: {
                            label: {
                                show: false
                            },
                            labelLine: {
                                show: false
                            },
                            color: 'rgb(60,60,60)',
                            borderColor: 'rgb(60,60,60)',
                            borderWidth: 5
                        }
                    }
                }]
            }]
        }
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }
    function echarts4() {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('tall'));
        // var shadowColor = '#374b86';
        var option = {
            title: {
                text: 60 + '%',
                left: 'center',
                top: 'center', //top待调整
                textStyle: {
                    color: '#019BFF',
                    fontSize: 26,
                    fontFamily: 'sans-serif'
                }
            },
            series: [{
                name: 'pie1',
                type: 'pie',
                clockWise: true,
                radius: ['70%', '80%'],
                itemStyle: {
                    normal: {
                        label: {
                            show: false
                        },
                        labelLine: {
                            show: false
                        }
                    }
                },
                hoverAnimation: false,
                data: [{
                    value: 60,
                    name: 'completed',
                    itemStyle: {
                        normal: {
                            borderWidth: 5,
                            borderColor: {
                                colorStops: [{
                                    offset: 0,
                                    color: '#E10B25' // 0% 处的颜色
                                }, {
                                    offset: 1,
                                    color: '#F15165' // 100% 处的颜色
                                }]
                            },
                            color: { // 完成的圆环的颜色
                                colorStops: [{
                                    offset: 0,
                                    color: '#E10B25' // 0% 处的颜色
                                }, {
                                    offset: 1,
                                    color: '#F15165' // 100% 处的颜色
                                }]
                            },
                            label: {
                                show: false
                            },
                            labelLine: {
                                show: false
                            }
                        }
                    }
                }, {
                    name: 'gap',
                    value: 100 - 60,
                    itemStyle: {
                        normal: {
                            label: {
                                show: false
                            },
                            labelLine: {
                                show: false
                            },
                            color: 'rgb(60,60,60)',
                            borderColor: 'rgb(60,60,60)',
                            borderWidth: 5
                        }
                    }
                }]
            }]
        }
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }
})